<?php
class page {
	var $firstPageStyle = '<font face="webdings">9</font>'; 
	var $prevPageStyle = '<font face="webdings">3</font>'; 
	var $nextPageStyle = '<font face="webdings">4</font>'; 
	var $lastPageStyle = '<font face="webdings">:</font>'; 
	var $pageCount; 
	var $recordCount; 
	var $pages; 
	var $currentPage = 1; 
	var $perpage; 
	var $showPageNum = 6; 
	var $pageUrl; 	

/* 
* @param: (int)$totalnum=记录总数 
* @param: (int)$curren_tpage=当前页 
* @param: (int)$perpage=每页显示记录数 
*/ 

function page($totalnum=0, $current_page=0, $perpage=20){ 
	$this->perpage = $perpage; 
	$this->pageCount = $totalnum / $perpage; 
	$this->recordCount = $totalnum; 
	if (isset($_GET['page'])){ 
		$this->currentPage = $_GET['page']; 
	} 
	
	$temStr = $this->pageCount - floor($this->pageCount); //判断总页数,将$this->recordCount与其整数部分相减，如果为0则不用处理，如果小于1则在$this->recordCount整数部分上加1
	if ($temStr==0){ 
		$this->pageCount = floor($this->pageCount); 
	} 
	if ($temStr<1 && $temStr>0){ 
		$this->pageCount = floor($this->pageCount) + 1; 
	} 
	$this->pageUrl = $this->ParseUrl(); 
	$this->pageSplit(); 
} 



function getStartRow(){ 
	return $this->perpage*($this->currentPage-1); 
} 

/* 
**页码显示计算 
*/ 

function pageSplit(){ 
	$firstPage=$prevPage=$nextPage=$lastPage="";
	$startPage = 1; 
	$endPage = $this->pageCount; 
	if ($this->pageCount > $this->showPageNum){ 
		$startPage = $this->currentPage - 2; 
		if ($startPage < 1 ){
			$startPage = 1;
		} 
		$endPage = $startPage + $this->showPageNum; 
		if ($endPage > $this->pageCount){ 
			$endPage = $this->pageCount; 
			$startPage = $endPage - $this->showPageNum; 
		} 
	} 
	if ($startPage > 1){ 
		$firstPage = ' <a href="'.$this->pageUrl.'page=1">'.$this->firstPageStyle.'</a>'; 
		$prevPage = ' <a href="'.$this->pageUrl.'page='.($this->currentPage - 1).'">'.$this->prevPageStyle.'</a>'; 
	} 
	if ($endPage < $this->pageCount){ 
		$lastPage = ' <a href="'.$this->pageUrl.'page='.$this->pageCount.'">'.$this->lastPageStyle.'</a>'; 
		$nextPage = ' <a href="'.$this->pageUrl.'page='.($endPage + 1).'">'.$this->nextPageStyle.'</a>'; 
	} 
	for ($i = $startPage ; $i <= $endPage;$i++){ 
		if ($this->currentPage == $i){ 
			$this->pages = ' <font color="red">'.$this->pages.$i.'</font>'; 
		}else{ 
			$this->pages = $this->pages . ' <a href="'.$this->pageUrl.'page='.$i.'">'.$i."</a> "; 
		} 
	} 
	$this->pages = $firstPage.$prevPage.$this->pages.$nextPage.$lastPage; 
} 

/* 
**处理URL参数 
*/ 

function ParseUrl(){ 
	$tempurl = $_SERVER['QUERY_STRING']; 
	$delPagePara='';
	if ($tempurl != ""){ 
		parse_str($tempurl,$paramArray); 
		if (array_key_exists('page',$paramArray)){ 
			$delPagePara = "page=".$paramArray['page']; 
			unset($paramArray['page']); 
		} 
		$tempurl = str_replace($delPagePara,"",$tempurl); 
		if (substr($tempurl,0,1)=="&"){ 
			$tempurl = substr($tempurl,1,strlen($tempurl)); 
		} 
		if (substr($tempurl,strlen($tempurl)-1,strlen($tempurl))=="&"){ 
			$tempurl = substr($tempurl,0,strlen($tempurl)-1); 
		} 
		$tempurl = str_replace("&&","&",$tempurl); 
	} 
	if (strlen($tempurl) > 0){
		$tempurl.="&";
	} 
	return basename($_SERVER['PHP_SELF'])."?".$tempurl; 
} 

/* 
**显示分页 
*/ 

function showPageList(){ 
	return " 总记录数：".$this->recordCount."&nbsp;&nbsp;页数：".$this->currentPage."/".$this->pageCount ." ". "第".$this->pages."页"; 
} 

}
?>